var app = angular.module('myApp');
var monArr = [];
app.controller('comicController', ['$scope', '$http', "$timeout",'navServ','$location', function($scope, $http, $timeout,navServ,$location) {
	var lis = document.querySelectorAll('#c-date>li');
	var cas = document.querySelectorAll('.c-a');
	$scope.week = function(num) {
		for(var i = 0; i < lis.length; i++) {
			lis[i].index = i;
			lis[i].style.borderBottom = 'none';
			cas[i].style.color = '#2A363D'
		}
		lis[num].style.borderBottom = '2px solid #ffd704';
		cas[num].style.color = '#ffd704';
		
		dianzanFn();
		yujiazai();
	}

	if($location.path() == '/comic/monday') {
			
			$scope.week(0);
		} else if($location.path() == '/comic/tuesday') {
			$scope.week(1);
		} else if($location.path() == '/comic/wednesday') {
			$scope.week(2);
		} else if($location.path() == '/comic/thursday') {
			$scope.week(3);
		} else if($location.path() == '/comic/friday') {
			$scope.week(4);
		} else if($location.path() == '/comic/saturday') {
			$scope.week(5);
		} else if($location.path() == '/comic/sunday') {
			$scope.week(6);
		}
	//周一-------------------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/monday.json'
	}).then(function(data) {
//		var monArr = [];
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				var $monimgs = $('<img>').attr('src', data.data.data.comics[i].cover_image_url);
				monArr.push($monimgs);
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat = data.data.data.comics;

	}, function(err) {
		console.log(err);
	});

	//周二-----------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/tuesday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat2 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	//周三--------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/wednesday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat3 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	//周四----------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/thursday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat4 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	//周五-----------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/friday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat5 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	//周六----------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/tuesday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat6 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	//周日--------------------------------------------------------------------------
	$http({
		method: 'GET',
		url: './json/sunday.json'
	}).then(function(data) {
		for(var i in data.data.data.comics) {
			if(data.data.data.comics[i].likes_count > 100000) {
				$scope.likes_count = parseInt(data.data.data.comics[i].likes_count / 10000) + '万';
				data.data.data.comics[i].likes_count = $scope.likes_count;
			}
		}
		$scope.dat7 = data.data.data.comics;
	}, function(err) {
		console.log(err);
	});

	dianzanFn();
	yujiazai();
	
	function yujiazai(){
		$timeout(function() {

		for(var j in monArr) {
			monArr[j].ready(function() {
				$($('.monimg')[j]).attr("src", $(monArr[j]).attr("src"));
			})
		}

		},100);
	}
	
	
	var flag = true;
	function dianzanFn() {
		$timeout(function() {
			$(".c-monday-img1").on("click", function() {
				if(flag) {
					flag = false;

					$(this).attr('src', './comic/img/ic_review_detail_liked.png').css('animation', 'myfirst 0.5s');
					var mon = this;
					setTimeout(function() {
						$(mon).css('animation', 'none');
					}, 510);
					var $monImg = $('c-monday-img1');
					var $imgParent = $(this).parent().children('.c-monday-s3');
					$imgParent.css('color', '#ffd704');
					if($imgParent.html() > 1000) {
						$imgParent.html(parseInt($imgParent.html()) + 1);
					}
				} else {
					flag = true;
					$(this).attr('src', './comic/img/ic_home_praise_normal.png').css('animation', 'myfirst 0.5s');
					var mon = this;
					setTimeout(function() {
						$(mon).css('animation', 'none');
					}, 510);
					var $monImg = $('c-monday-img1');
					var $imgParent = $(this).parent().children('.c-monday-s3');
					$imgParent.css('color', '#777777');
					if($imgParent.html() > 1000) {
						$imgParent.html(parseInt($imgParent.html()) - 1);
					}
				}
			});
		}, 100);
	}
	
	
	
	$timeout(function(){
            //记录提示栏初始位置
        var height = $('#c-date').offset().top;

      	$(window).on("scroll",function(){

	        var scrollTop = $(this).scrollTop();
			$('#c-zhanwei').css('display','block');
	        scrollTop - height >=0 ?
			
	        $('#c-date').css({"top":0,"position":"fixed"}):
	        $('#c-date').css({"position":"absolute","top":"1.1rem"});
    	});
	})

}]);